#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include "../Utilities/utilities.h"
#include "cModel.h"
#include "utils.h"

cModel::cModel(): m_aVertices(NULL), m_aIndices(NULL),
m_iNumVertices(0), m_iNumIndices(0)
{
}

cModel::~cModel()
{
	SAFE_DEL_ARRAY(m_aVertices);
	SAFE_DEL_ARRAY(m_aIndices);
}

void cModel::LoadModel(char* modelPath)
{
	FILE * pFile;
	fopen_s (&pFile, modelPath, "r" );
	
	if (pFile==NULL) 
	{
		fputs ("File error: ",stderr);
		LOGE("Cannot open file %s\n", modelPath);
		return;
	}

		CHAR buffer[MAX_LENGTH];

		// Read number of vertices
		ReadLine(pFile, buffer);
		int iPos = ScanNumberInt32(buffer, &m_iNumVertices, 0);
		m_aVertices = new Vertex[m_iNumVertices];

		// Read vertices data
		int iIndex;
		int iID;
		for (iIndex = 0; iIndex < m_iNumVertices; ++iIndex)
		{
			ReadLine(pFile, buffer);
			
			// ID
			iPos = ScanNumberInt32(buffer, &iID, 0); 

			// Position
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.x, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.y, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.z, iPos); 

			// Normal
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.x, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.y, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.z, iPos); 

			// Binormal
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].binormal.x, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].binormal.y, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].binormal.z, iPos); 

			// Tangent
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.x, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.y, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.z, iPos);

			// Texcoord
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].texcoord.x, iPos); 
			iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].texcoord.y, iPos); 
		}

		//for (iIndex = 0; iIndex < m_iNumVertices; ++iIndex)
		//	LOGI("%f %f %f\n", m_aVertices[iIndex].position.x, m_aVertices[iIndex].position.y, m_aVertices[iIndex].position.z);

		// Read number of Indices
		ReadLine(pFile, buffer);
		ScanNumberInt32(buffer, &m_iNumIndices);

		m_aIndices = new unsigned int[m_iNumIndices];
		m_iNumFaces = m_iNumIndices/3;
		
		// Read Indices data
		for (iIndex = 0; iIndex < m_iNumFaces; ++iIndex)
		{
			ReadLine(pFile, buffer);
			
			// ID
			iPos = ScanNumberInt32(buffer, &iID, 0); 

			iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3]    , iPos); 
			iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3 + 1], iPos); 
			iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3 + 2], iPos);
		}

		//for (iIndex = 0; iIndex < m_iNumFaces; ++iIndex)
		//	LOGI("%d %d %d\n", m_aIndices[iIndex* 3], m_aIndices[iIndex* 3 + 1], m_aIndices[iIndex* 3 + 2]);

		// Close file
		fclose (pFile);

}